#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    batSet = NULL;

    s1 = new SelectDevice(this);
    cali = new Cali(this);


    ui->stackedWidget->addWidget(s1);
    ui->stackedWidget->addWidget(cali);
    ui->stackedWidget->setCurrentIndex(0);

    com = new Serial_com();
    com->moveToThread(&comT);
    comT.start();

    connect(this, &MainWindow::MainClose, com, &Serial_com::MainClose);

    connect(this, &MainWindow::SendCom, com, &Serial_com::writeMyCom, Qt::QueuedConnection);
    connect(this, &MainWindow::InitCom, com, &Serial_com::com_init, Qt::QueuedConnection);
    connect(com, &Serial_com::Read_Com, s1, &SelectDevice::com_rec, Qt::QueuedConnection);

    connect(cali, &Cali::exit_set, this, &MainWindow::exitCali);
    connect(cali, &Cali::SendSet, this, &MainWindow::info_set_send, Qt::QueuedConnection);
    connect(s1, &SelectDevice::openDevice, this, &MainWindow::select_Open);
    connect(s1, &SelectDevice::SendSet, this, &MainWindow::info_set_send, Qt::QueuedConnection);
    connect(s1, &SelectDevice::openCali, this, &MainWindow::openCali);

    serialScan = new QTimer(this);
    connect(serialScan, &QTimer::timeout, this, &MainWindow::comScan);
    serialScan->start(1000);
    comScan();
}

MainWindow::~MainWindow()
{
    qDebug() << "Main emit";
    comT.quit();
    comT.wait();
    qDebug() << "~Main Close" << comT.isFinished();
    while(!comT.isFinished())
    {
        QThread::msleep(500);
        qDebug() << "~Main Close" << comT.isFinished();
    }
    delete ui;
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    emit MainClose();
}

void MainWindow::info_set_send(QByteArray ba)
{
    QThread::msleep(100);
    emit SendCom(ba);
}

void MainWindow::select_Open(int currentSelect)
{
    if(batSet == NULL)
    {
        batSet = new BatterySet(currentSelect);
        ui->stackedWidget->addWidget(batSet);
        ui->stackedWidget->setCurrentIndex(2);

        connect(com, &Serial_com::Read_Com, batSet, &BatterySet::com_rec, Qt::QueuedConnection);
        connect(batSet, &BatterySet::exit_set, this, &MainWindow::close_set, Qt::QueuedConnection);
        connect(batSet, &BatterySet::SendSet, this, &MainWindow::info_set_send, Qt::QueuedConnection);
    }
}

void MainWindow::openCali()
{
    ui->stackedWidget->setCurrentIndex(1);
}

void MainWindow::exitCali()
{
    ui->stackedWidget->setCurrentIndex(0);
}

void MainWindow::close_set()
{
    if(batSet != NULL)
    {
        ui->stackedWidget->setCurrentIndex(0);
        ui->stackedWidget->removeWidget(batSet);

        disconnect(com, &Serial_com::Read_Com, batSet, &BatterySet::com_rec);
        disconnect(batSet, &BatterySet::exit_set, this, &MainWindow::close_set);
        disconnect(batSet, &BatterySet::SendSet, this, &MainWindow::info_set_send);
        delete batSet;
        batSet = NULL;
    }
}

void MainWindow::comScan()
{
    QList<QSerialPortInfo> serialList;
    serialList.clear();
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        serialList.append(info);
    }

    if(comList.count() != serialList.count())
    {
        comList = serialList;
        ui->comboBox->clear();
        foreach (QSerialPortInfo info, comList) {
            ui->comboBox->addItem(info.portName());
        }
        qDebug() << "diff";
    }else{
        for(int i=0; i < serialList.count(); i++)
        {
            if(comList.at(i).portName() != serialList.at(i).portName())
            {
                comList.clear();
                comList = serialList;
                ui->comboBox->clear();
                foreach (QSerialPortInfo info, comList) {
                    ui->comboBox->addItem(info.portName());
                }
                break;
            }
        }
    }
}

void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1)
{
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        if(info.portName() == arg1)
            ui->label_2->setText(info.description());
    }
}

void MainWindow::on_pushButton_clicked()
{
    if(com->myCom == NULL)
    {
        emit InitCom(ui->comboBox->currentText());
        ui->pushButton->setText("关闭串口");
    }else if(com->myCom != NULL)
    {
        com->ComClose();
        ui->pushButton->setText("打开串口");
    }
}
